const {
  Mysql,
  addSql,
  xiuSql,
  remSql,
  execute,
  paraphrase,
  compare,
} = require("../../db/db");
let login = async (ctx, next) => {
  //
  // 具体逻辑
  // 连接数据库后进行的增删改查
  // 返回结果

  //登录请求
  console.log("我是测试接口");
  let { phone, password, type } = ctx.request.body;
  //   addSql("userlist", { name: `phone_${phone}`, phone, password });
  let res = await Mysql("userlist", { phone });
  let userData = JSON.parse(JSON.stringify(res))[0];
  console.log("----------------------------------");
  console.log(res, userData, type, ctx.request.body);
  //验证手机号是否存在
  if (res.length !== 0) {
    //   如果该账户存在，判断密码是否正确
    if (type === "01") {
      if (userData.password == password) {
        // 如果密码也正确
        // 允许登录
        ctx.body = {
          msg: "管理员登陆成功",
          code: "01",
          data: userData,
        };
      } else {
        // 如果密码错误,提示对应信息
        ctx.body = {
          msg: "密码错误",
          code: "03",
        };
      }
    } else if (type === "02_1") {
      //type为02_1代表点击的是忘记密码的下一步
      //此时当前手机号存在数据库
      //允许执行下一步
      console.log("已查找到用户");
      ctx.body = {
        msg: "该账户已注册,请返回登录叭。",
        code: "Yes",
      };
    } else if (type === "02_2") {
      //type为02_2代表点击的是忘记密码的提交
      //此时当前手机号存在数据库
      //修改查找到的用户的修改密码
      console.log("成功接收修改密码请求");
      let data = ctx.request.body;
      delete data.type;
      xiuSql("userlist", data, { name: userData.name });
      ctx.body = {
        msg: "修改密码已完成,请返回登录",
        code: "setYes",
      };
    } else if (type === "03") {
      //type为03代表点击的是注册的提交
      //此时当前手机号存在数据库
      //提示用户账户已存在
      ctx.body = {
        msg: "该账户已注册,请返回登录叭。",
        code: "Yes",
      };
    }
  } else if (type === "03_1") {
    // 已进入注册页面
    // 点击提交按钮
    // 直接添加
    let data = ctx.request.body;
    delete data.type;
    data.name = `phone_${phone}`;
    addSql("userlist", data);
    ctx.body = {
      msg: "注册成功",
      code: "02",
    };
  } else {
    if (type === "01") {
      //type为01代表点击的是登录
      //此时当前手机号不存在数据库
      //禁止登陆
      ctx.body = {
        msg: "无效账户",
        code: "02",
      };
    } else if (type === "02_1") {
      //type为02_1代表点击的是忘记密码的下一步
      //此时当前手机号不存在数据库
      //不允许执行下一步
      ctx.body = {
        msg: "没有找到您的账户喔",
        code: "No",
      };
    } else if (type === "03") {
      //type为03代表点击的是注册的提交
      //此时当前手机号不存在数据库
      ctx.body = {
        msg: "该账号不存在,可以注册",
        code: "No",
      };
    }
  }

  await next();
};
// 获取所有商品数据
let getshoppingList = async (ctx, next) => {
  // 接收积分
  let {} = ctx.request.body;
  let res = [];
  console.log(ctx.request.body);
  res = await Mysql("goods");
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
// 获取所用客户端用户数据
let getUserlist = async (ctx, next) => {
  // 接收积分
  let {} = ctx.request.body;
  let res = [];
  console.log(ctx.request.body);
  res = await execute(`SELECT * from user_message where uid!=35`);
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
// 获取聊天记录
let getMessage = async (ctx, next) => {
  // 接收积分
  let { uid, Toid } = ctx.request.body;
  let res = [];
  let res_2 = [];
  console.log(ctx.request.body);
  res = await execute(
    `SELECT * from message where uid=${uid} and Toid=${Toid}`
  );
  res_2 = await execute(
    `SELECT * from message where uid=${Toid} and Toid=${uid}`
  );
  res.push(...res_2);
  res.forEach((item, idnex) => {
    item.createTime = Number(item.createTime);
  });

  res = res.sort(compare("createTime"));
  console.log(paraphrase(res));

  ctx.body = {
    msg: "获取成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
module.exports = {
  login,
  getshoppingList,
  getUserlist,
  getMessage,
};
